package RobotPlayer
{
	import BasicEngine.Vector2D;
	import flash.geom.Point;
	import flash.utils.Dictionary;
	import GameLogic.DataSource;
	import GameLogic.Game;
	/**
	 * 机器人工厂
	 * 负责机器人的建造,改装,销毁管理
	 */
	public class RobotFactory
	{
		public static var RobotRecords:Dictionary;
		
		private static var incrmentIndex:int = 1000;
		
		public static var BaseRobotMass:int = 1500;
		public static var BaseSpeed:int = 12;
		public static var BaseForce:int  = 12;
		
		public static var RobotNums:int = 0;
		public static const HeroID:int = 999;
		
		
		public function RobotFactory()
		{
		}
		
		public static  function Init():void
		{
			RobotRecords = new Dictionary();
			DataSource.InitSt();
		}
		
		public static function CreatePlayer(_x:int,_y:int):BaseRobot
		{
			var player:BaseRobot = MakeRobot();
			player.Pos.X = _x;
			player.Pos.Y = _y;
			Game.PlayerCanvas.addChild(player.Canvas);
			player.ID = HeroID;
			player.ShowName.text = "Hero:" + player.ID.toString();
			player.ShowName.textColor = 0xffff00;
			RobotRecords[player.ID] = player;									
			Game.ShowRadar.CreateAvatarPoint(player.ID);
			return player;
		}
		
		public static function CreateRobotNameColor(_x:int,_y:int,_name:String,_clr:int):BaseRobot
		{
			incrmentIndex++;
			var player:BaseRobot = MakeRobot();
			player.Pos.X = _x;
			player.Pos.Y = _y;
			Game.PlayerCanvas.addChild(player.Canvas);
			player.ID = incrmentIndex;
			player.ShowName.text = _name + player.ID.toString();
			player.ShowName.textColor = _clr;
			RobotRecords[player.ID] = player;									
			Game.ShowRadar.CreateAvatarPoint(player.ID);
			return player;
		}
		
		/**
		 * 创建机器人
		 */
		public static function CreateRobot():BaseRobot
		{
			var x:int = Math.random() * Game.Width;
			var y:int = Math.random() * Game.Height;
			var robot:BaseRobot = CreateRobotNameColor(x, y, "Robot", 0xffffff);
			return robot;
		}
		
		/**
		 * 装配零件
		 */
		public static function AssemblyPart(_robot:BaseRobot, _moduleName:String, _x:int, _y:int, _part:RobotPart):void
		{
			var module:RobotModule = _robot.Modules[_moduleName];
			module.AddPart(_part, new Point(_x, _y));
		}
		/**
		 * 卸载零件
		 */
		public static function RemovePart(_robot:BaseRobot,_moduleName:String, _partName:String):void
		{
			var module:RobotModule = _robot.Modules[_moduleName];
			module.RemovePart(_partName);
		}
		/**
		 * 根据ID返回机器人
		 */
		public static function GetRobot(_id:int):BaseRobot
		{
			return RobotRecords[_id];
		}
		
		public static function GetHero():BaseRobot
		{
			return RobotRecords[HeroID];
		}
		
		/**
		 * 删除机器人
		 */
		public static function RemoveRobot(_id:int):void
		{
			var robot:BaseRobot = GetRobot(_id);
			if (robot != null)
			{
				Game.PlayerCanvas.removeChild(robot.Canvas);
				robot.Dispose();
				delete RobotRecords[_id];
				Game.ShowRadar.RemoveAvatarPoint(_id);
			}
		}
		
		private static function MakeRobot():BaseRobot
		{
			var part:RobotPart = new RobotPart("LeftCannon",-18,-20,0.6);
			part.AddStateData("开火", 100, [1043, 1044, 1044, 1045, 1046], true, false);
			var part2:RobotPart = new RobotPart("RightCannon",-18,-20,0.6);
			part2.AddStateData("开火", 100, [1043, 1044, 1044,1045, 1046],true,false);
			var part3:RobotPart = new RobotPart("Cockpit",-16,-20);
			part3.AddStateData("正常状态", 100, [1014],true,true);
			var part4:RobotPart = new RobotPart("Chassis",-23, -20);
			part4.AddStateData("正常状态", 40, [1022,1023,1024,1025,1027,1028,1029,1030,1031,1032,1033,1035,1036,1037],true);
			var BottomModule:RobotModule = new RobotModule("BottomModule", 0, 0);
			
			BottomModule.AddPart(part4, new Point(0, 0),90);
			var TopModule:RobotModule = new RobotModule("TopModule",0,0,0,0.7);
			TopModule.AddPart(part3, new Point(0, 0),-90);
			TopModule.AddPart(part, new Point(0,-20),-90);
			TopModule.AddPart(part2, new Point(0,20),-90);
			TopModule.HideAssist = true;
			var robot:BaseRobot = new BaseRobot(new Vector2D(0, 0), 4500, 0.2,0.2);
			robot.AssemblyModule(BottomModule);
			robot.AssemblyModule(TopModule);
			
			return robot;			
		}
	}
}